Skip to content

Added calculator app#1110

Open
asthanit1205 wants to merge 1 commit intoflorinpop17:masterfrom
asthanit1205:feature-calculator
Open

Added calculator app#1110
asthanit1205 wants to merge 1 commit intoflorinpop17:masterfrom
asthanit1205:feature-calculator

Conversation

@asthanit1205
Copy link
Copy Markdown

@asthanit1205 asthanit1205 commented Apr 8, 2026

Feature: Calculator App

What I implemented:

  • Basic calculator UI
  • Supports +, -, *, /
  • Clear (C) and All Clear (AC) buttons

Files added:

  • calculator-app/index.html
  • calculator-app/style.css
  • calculator-app/script.js

How to test:

  • Open index.html
  • Click buttons to perform calculations
Screenshot 2026-04-09 013833

Summary by CodeRabbit

New Features

  • Added a new calculator application with an interactive interface
  • Supports basic arithmetic operations (addition, subtraction, multiplication, division) and numeric input
  • Includes functionality to clear all input and delete the last entered character
  • Displays calculation results with error handling

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 8, 2026

📝 Walkthrough

Walkthrough

A new calculator web application is introduced with three files: HTML providing the user interface with a display and button grid, JavaScript implementing calculator operations (number input, arithmetic operators, clear functions, and expression evaluation), and CSS defining layout styling.

Changes

Cohort / File(s) Summary
Calculator Web Application
calculator-app/index.html, calculator-app/script.js, calculator-app/style.css
Introduces a complete calculator with HTML UI (disabled text input for display and button grid with inline onclick handlers), JavaScript logic (press, calculate, clearAll, clearLast functions for user interaction and evaluation), and CSS styling (200px fixed-width centered container with 40x40px buttons).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Hop-hop, the buttons align,
Add, subtract, numbers combine!
A calculator shines so bright,
This new app feels just right!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Added calculator app' directly and clearly summarizes the main change: a new calculator application has been added with UI, styling, and functionality.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
calculator-app/style.css (1)

6-15: Use an explicit grid for deterministic button layout.

Right now row wrapping is implicit. Defining .buttons as a grid will prevent layout drift across font/zoom/container tweaks.

♻️ Proposed CSS update
 .calculator {
   width: 200px;
   margin: auto;
 }
 
+.buttons {
+  display: grid;
+  grid-template-columns: repeat(4, 40px);
+  gap: 4px;
+  justify-content: center;
+}
+
 button {
   width: 40px;
   height: 40px;
-  margin: 2px;
+  margin: 0;
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@calculator-app/style.css` around lines 6 - 15, The button layout should use
an explicit CSS grid: add a .buttons rule (targeting the container that holds
the button elements inside .calculator) and set display: grid, a fixed
grid-template-columns (e.g., repeat(4, 1fr) for four columns) and a gap for
spacing so buttons no longer rely on inline wrapping; keep existing button size
rules but remove reliance on implicit wrapping so classes .calculator, .buttons
and button are used to enforce deterministic layout across zoom/font changes.
calculator-app/index.html (1)

13-33: Move inline onclick handlers to JS event listeners.

All buttons at lines 13-33 use inline handlers. Moving them to JavaScript event listeners improves maintainability and supports stricter Content Security Policy configurations.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@calculator-app/index.html` around lines 13 - 33, Replace inline onclick
attributes on all buttons by adding data-action or distinct IDs and wiring them
up in your JS: query-select the buttons (e.g., buttons with data-action="digit",
data-action="operator", data-action="clear-all", data-action="clear-last",
data-action="equals") and attach event listeners that call the existing
functions clearAll, clearLast, press (pass the button value), and calculate
respectively; remove the onclick attributes from the HTML so behavior is driven
by addEventListener calls and use event.currentTarget or dataset to get the
button value when invoking press.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@calculator-app/index.html`:
- Line 10: Replace the input attribute disabled with readonly on the element
with id "display" so the field remains focusable/selectable; update any related
styling or JS that checks disabled (e.g., code referencing
document.getElementById("display").disabled) to instead handle readonly if
present and ensure ARIA/labeling still applies for accessibility.

In `@calculator-app/script.js`:
- Around line 3-10: The calculate() function currently uses eval(display.value)
which is unsafe; replace it with a safe expression evaluator that only accepts
digits, whitespace, parentheses and the four basic operators (+, -, *, /) and
rejects any other characters, then parse and compute the result (e.g., implement
a tokenizer + shunting-yard or simple recursive descent evaluator) and assign
the numeric result back to display.value; also ensure press() only allows adding
valid tokens or update calculate() to re-validate/sanitize display.value before
parsing and handle parse errors in the existing try/catch.

---

Nitpick comments:
In `@calculator-app/index.html`:
- Around line 13-33: Replace inline onclick attributes on all buttons by adding
data-action or distinct IDs and wiring them up in your JS: query-select the
buttons (e.g., buttons with data-action="digit", data-action="operator",
data-action="clear-all", data-action="clear-last", data-action="equals") and
attach event listeners that call the existing functions clearAll, clearLast,
press (pass the button value), and calculate respectively; remove the onclick
attributes from the HTML so behavior is driven by addEventListener calls and use
event.currentTarget or dataset to get the button value when invoking press.

In `@calculator-app/style.css`:
- Around line 6-15: The button layout should use an explicit CSS grid: add a
.buttons rule (targeting the container that holds the button elements inside
.calculator) and set display: grid, a fixed grid-template-columns (e.g.,
repeat(4, 1fr) for four columns) and a gap for spacing so buttons no longer rely
on inline wrapping; keep existing button size rules but remove reliance on
implicit wrapping so classes .calculator, .buttons and button are used to
enforce deterministic layout across zoom/font changes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0ebbbb09-4089-4aa5-bd9c-8efffb2112b0

📥 Commits

Reviewing files that changed from the base of the PR and between 9e8dd00 and a88bab9.

📒 Files selected for processing (3)
  • calculator-app/index.html
  • calculator-app/script.js
  • calculator-app/style.css

Comment thread calculator-app/index.html
<body>

<div class="calculator">
<input type="text" id="display" disabled />
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Prefer readonly over disabled for the display field.

disabled prevents focus/selection and is less accessible for a calculator display.

✅ Suggested HTML tweak
-  <input type="text" id="display" disabled />
+  <input type="text" id="display" readonly aria-label="Calculator display" />
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<input type="text" id="display" disabled />
<input type="text" id="display" readonly aria-label="Calculator display" />
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@calculator-app/index.html` at line 10, Replace the input attribute disabled
with readonly on the element with id "display" so the field remains
focusable/selectable; update any related styling or JS that checks disabled
(e.g., code referencing document.getElementById("display").disabled) to instead
handle readonly if present and ensure ARIA/labeling still applies for
accessibility.

Comment thread calculator-app/script.js
Comment on lines +3 to +10
function press(val) {
display.value += val;
}

function calculate() {
try {
display.value = eval(display.value); // (ok for practice)
} catch {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify there is no remaining use of eval in JS files.
rg -nP --type=js '\beval\s*\('

Repository: florinpop17/app-ideas

Length of output: 153


Remove eval from expression execution.

eval on calculator input is a security blocker. Replace it with a parser/evaluator limited to digits/operators.

🔒 Safer approach (no dynamic code execution)
+function evaluateExpression(expression) {
+  const sanitized = expression.replace(/\s+/g, "");
+  const tokens = sanitized.match(/\d+(\.\d+)?|[()+\-*/]/g);
+  if (!tokens || tokens.join("") !== sanitized) throw new Error("Invalid expression");
+
+  const prec = { "+": 1, "-": 1, "*": 2, "/": 2 };
+  const output = [];
+  const ops = [];
+
+  for (const t of tokens) {
+    if (/^\d/.test(t)) output.push(Number(t));
+    else if (t in prec) {
+      while (ops.length && ops[ops.length - 1] in prec && prec[ops[ops.length - 1]] >= prec[t]) {
+        output.push(ops.pop());
+      }
+      ops.push(t);
+    } else if (t === "(") ops.push(t);
+    else if (t === ")") {
+      while (ops.length && ops[ops.length - 1] !== "(") output.push(ops.pop());
+      if (ops.pop() !== "(") throw new Error("Mismatched parentheses");
+    }
+  }
+  while (ops.length) {
+    const op = ops.pop();
+    if (op === "(") throw new Error("Mismatched parentheses");
+    output.push(op);
+  }
+
+  const stack = [];
+  for (const t of output) {
+    if (typeof t === "number") stack.push(t);
+    else {
+      const b = stack.pop();
+      const a = stack.pop();
+      if (a === undefined || b === undefined) throw new Error("Invalid expression");
+      stack.push(t === "+" ? a + b : t === "-" ? a - b : t === "*" ? a * b : a / b);
+    }
+  }
+  if (stack.length !== 1) throw new Error("Invalid expression");
+  return stack[0];
+}
+
 function calculate() {
   try {
-    display.value = eval(display.value); // (ok for practice)
+    display.value = String(evaluateExpression(display.value));
   } catch {
     display.value = "ERR";
   }
 }
🧰 Tools
🪛 Biome (2.4.10)

[error] 9-9: eval() exposes to security risks and performance issues.

(lint/security/noGlobalEval)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@calculator-app/script.js` around lines 3 - 10, The calculate() function
currently uses eval(display.value) which is unsafe; replace it with a safe
expression evaluator that only accepts digits, whitespace, parentheses and the
four basic operators (+, -, *, /) and rejects any other characters, then parse
and compute the result (e.g., implement a tokenizer + shunting-yard or simple
recursive descent evaluator) and assign the numeric result back to
display.value; also ensure press() only allows adding valid tokens or update
calculate() to re-validate/sanitize display.value before parsing and handle
parse errors in the existing try/catch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant